<?php
    require('./Utils.php');

    $post = getJsonParamter();
    $conn  = getConnection();

    $username = $post['username'];
    $pet_id = $post['pet_id'];
    $number = $post['number'];

    $sql = "SELECT pet_id, user_id, number FROM cart WHERE user_id = ? AND pet_id = ?";

    $data = query($conn, $sql, $username, $pet_id);

    $sql = "INSERT INTO cart(number, pet_id, user_id) VALUES (?, ?, ?)";

    if($data['number']){
        $number = $number + $data['number'];

        $sql = "UPDATE cart SET number = ? WHERE pet_id = ? AND user_id = ?";

    }


    $result = execute($conn, $sql, $number, $pet_id, $username);

    if($result) getJson(200, '添加购物车成功', null);

    else getJson(500, '添加购物车失败', null);
?>